import { RouteRecordRaw } from "vue-router";
import dashboardRoutes, { dashboardBreadcrumb } from "./dashboard";

// 匹配名称
const matchPath = (path: string = "") => {
  const obj = {
    base: "基础",
    button: "按钮",
    card: "卡片",
    breadcrumb: "面包屑",
    dropdown: "下拉菜单",
    dialog: "弹窗",
    drawer: "抽屉",
    pagination: "分页器",
    tabs: "页签",

    form: "表单",
    input: "输入框",
    select: "选择器",
    radio: "单选框",
    checkbox: "复选框",
    "date-picker": "时间选择器",
    "input-number": "数字输入框",
    cascader: "级联选择器",

    advance: "高级",
    table: "表格",

    custom: "自定义",
    "dynamic-form": "动态表单",
    "search-bar": "搜索栏",
  };

  return obj[path] || path;
};

// 生成 views 目录的对应路由，components 目录下的 vue 文件忽略
const createViewRoutes: (floder: string) => RouteRecordRaw[] = (
  floder = ""
) => {
  // require.context 第一个参数不能为变量
  const pages = (<any>require).context("@/views", true, /\.vue$/);
  const routes: RouteRecordRaw[] = [];
  const pageKeys = pages
    .keys()
    .filter((p) => p.includes(`./${floder}/`) && !p.includes("components/"));

  console.log(pageKeys);

  pageKeys.forEach((key) => {
    const path = key.split("/")[2];
    const name = matchPath(path);

    routes.push({
      path: `/${path}`,
      component: () => import(`@/views/${floder}/${path}/${path}.vue`),
      children: [],
      meta: {
        title: name,
        isKeepAlive: true,
        breadcrumbList: [
          ...dashboardBreadcrumb,
          { name: matchPath(floder), code: "" },
          { name: name, code: path },
        ],
        icon: "icon-shouye1-copy",
      },
    });
  });

  return routes;
};

// 生成 views 所有的路由
const floders = ["base", "form", "advance", "custom"];
const createAllRoutes = () => {
  return floders.map((floder) => {
    return {
      title: matchPath(floder),
      path: `/${floder}`,
      children: [...createViewRoutes(floder)],
    };
  });
};

export default [...dashboardRoutes, ...createAllRoutes()];
